package com.inforich.projectManage.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.ApproveEnum;
import com.inforich.common.enums.StageEnum;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.domain.ProjectRubbishDetail;
import com.inforich.projectManage.domain.ProjectRubbishExpand;
import com.inforich.projectManage.dto.ProjectApproveDTO;
import com.inforich.projectManage.mapper.ProjectRubbishExpandMapper;
import com.inforich.projectManage.service.IProjectRubbishDetailService;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 新扩改登记Service业务层处理
 *
 * @author hjm
 * @date 2022-10-30
 */
@Service
public class ProjectRubbishExpandServiceImpl extends
    ServiceImpl<ProjectRubbishExpandMapper, ProjectRubbishExpand> implements
    IProjectRubbishExpandService {

  @Autowired
  private IProjectRubbishDetailService projectRubbishDetailService;

  @Override
  public Page<ProjectRubbishExpand> pageProjectRubbishExpand(Page<ProjectRubbishExpand> page,
      Map<String, Object> map) {
    return baseMapper.pageProjectRubbishExpand(page, map);
  }

  @Override
  public List<ProjectRubbishExpand> listProjectRubbishExpand(Map<String, Object> map) {
    return baseMapper.listProjectRubbishExpand(map);
  }

  @Override
  public ProjectRubbishExpand getProjectRubbishExpandById(String id) {
    ProjectRubbishExpand pre = baseMapper.selectProjectRubbishExpandById(id);
    List<ProjectRubbishDetail> rdList = projectRubbishDetailService.listProjectRubbishDetailByProjectId(
        pre.getProjectId());
    pre.setRubbishDetailList(CollUtil.isEmpty(rdList) ? Lists.newArrayList() : rdList);
    return pre;
  }

  @Override
  public ProjectRubbishExpand getProjectRubbishExpandByProjectId(String projectId) {
    ProjectRubbishExpand pre = baseMapper.getProjectRubbishExpandByProjectId(projectId);
    if (pre == null) {
      return pre;
    }
    List<ProjectRubbishDetail> rdList = projectRubbishDetailService.listProjectRubbishDetailByProjectId(
        pre.getProjectId());
    // 根据阶段分组
    Map<String, List<ProjectRubbishDetail>> rdMap = rdList.stream()
        .collect(Collectors.groupingBy(ProjectRubbishDetail::getRubbishStage));
    pre.setRubbishBaseList(rdMap.get(StageEnum.BASE_STAGE.getDbData()));
    pre.setRubbishMainList(rdMap.get(StageEnum.MAIN_STAGE.getDbData()));
    pre.setRubbishDecorateList(rdMap.get(StageEnum.DECORATE_STAGE.getDbData()));
    pre.setRubbishSubmitList(rdMap.get(StageEnum.SUBMIT_STAGE.getDbData()));
    return pre;
  }

  @Override
  public AjaxResult approve(ProjectApproveDTO dto) {
    Integer approvalStatus =
        ApproveEnum.APPROVE_STATUS_SUCCESS.equals(dto.getStatus()) ?
            ApproveEnum.PASS.getDbData() : ApproveEnum.REJECT.getDbData();
    boolean result = update(new LambdaUpdateWrapper<ProjectRubbishExpand>()
        .set(ProjectRubbishExpand::getApprovalStatus, approvalStatus)
        .set(ProjectRubbishExpand::getApprovalDate, new Date())
        .set(ProjectRubbishExpand::getApprovalDesc, dto.getApprovalDesc())
        .eq(ProjectRubbishExpand::getId, dto.getId())
        .eq(ProjectRubbishExpand::getApprovalStatus, ApproveEnum.REVIEW.getDbData()));
    return result ? AjaxResult.success() : AjaxResult.error();
  }

  @Override
  public int countByProjectId(String projectId) {
    return this.count(new LambdaQueryWrapper<ProjectRubbishExpand>()
        .eq(ProjectRubbishExpand::getProjectId, projectId));
  }

  @Override
  public void removeByProjectId(String projectId) {
    remove(new LambdaQueryWrapper<ProjectRubbishExpand>()
        .eq(ProjectRubbishExpand::getProjectId, projectId));
  }


}
